package com.item.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import com.mobile.api.proto.MSystem.MTerm;
import com.mobile.api.proto.MSystem.MTermList;

/**
 * 生成学期列表等等与时间有关的文本信息	2014-7-4
 * @author yjh
 *
 */
public class TimeInfoGenerator {
	private InfoGenerator infoGenerator;
	public static SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
	public static SimpleDateFormat formatSimpleDate = new SimpleDateFormat("MM-dd");
	public TimeInfoGenerator(InfoGenerator infoGenerator) {
		super();
		this.infoGenerator = infoGenerator;
	}
	/**
	 * 学期信息类	2014-7-8
	 * @author yjh
	 *
	 */
	public static class TimeInfo {
		private String listInfo = "";	//e.g. 2013-2014学年第一学期
		private String termUrl = "";
		private int termNum;	//1 or 2
		private int termStart;	//e.g. "2013-2014"'s 2013 
		private int termEnd;	//e.g. "2013-2014"'s 2014
		public String getListInfo() {
			return listInfo;
		}
		public void setListInfo(String listInfo) {
			this.listInfo = listInfo;
		}
		public int getTermNum() {
			return termNum;
		}
		public void setTermNum(int termNum) {
			this.termNum = termNum;
		}
		public int getTermStart() {
			return termStart;
		}
		public void setTermStart(int termStart) {
			this.termStart = termStart;
		}
		public int getTermEnd() {
			return termEnd;
		}
		public void setTermEnd(int termEnd) {
			this.termEnd = termEnd;
		}
		public String getTermUrl() {
			return termUrl;
		}
		public void setTermUrl(String termUrl) {
			this.termUrl = termUrl;
		}
		
	}
	
	/**
	 * 根据TimeInfo类的字段生成url字符串
	 * @author yjh
	 *
	 */
	public static interface InfoGenerator {
		public void setInfo(TimeInfo info);
		public void setUrl(TimeInfo info);
	}
	/**
	 * 获取学期列表
	 * @param appid
	 * @param num
	 * @return
	 */
	public TimeInfo getTermListName(int num) {
		TimeInfo result = new TimeInfo();
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		if(month >= 8) {
			if(num % 2 == 1) {
				result.setTermStart(year - num / 2);
				result.setTermEnd(year + 1 - num / 2);
				result.setTermNum(1);
			} else {
				result.setTermStart(year - num / 2);
				result.setTermEnd(year + 1 - num / 2);
				result.setTermNum(2);
			}
		} else {
			if(num % 2 == 1) {
				result.setTermStart(year - 1 - num / 2);
				result.setTermEnd(year - num / 2);
				result.setTermNum(2);
			} else {
				result.setTermStart(year - num / 2);
				result.setTermEnd(year + 1 - num / 2);
				result.setTermNum(1);
			}
		}
		this.infoGenerator.setInfo(result);
		this.infoGenerator.setUrl(result);
		return result;
	}
	
	public InfoGenerator getInfoGenerator() {
		return infoGenerator;
	}
	public void setInfoGenerator(InfoGenerator infoGenerator) {
		this.infoGenerator = infoGenerator;
	}
	
	
	static public int transferHanNum(String temp) {
		int result = 0;
		if(temp.equals("一"))
			return 1;
		if(temp.equals("二"))
			return 2;
		if(temp.equals("三"))
			return 3;
		if(temp.equals("四"))
			return 4;
		if(temp.equals("五"))
			return 5;
		if(temp.equals("六"))
			return 6;
		if(temp.equals("日"))
			return 7;
		return result;
	}
	static public enum HanNum {
		N1(1, "一"),
		N2(2, "二"),
		N3(3, "三"),
		N4(4, "四"),
		N5(5, "五"),
		N6(6, "六"),
		N7(7, "日");
		private int code;
		private String str;
		private HanNum(int code, String str) {
			this.code = code;
			this.str = str;
		}
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getStr() {
			return str;
		}
		public void setStr(String str) {
			this.str = str;
		}
		public String toString() {
			return this.str;
		}
	}
	
	/**
	 * 产生只有一个项的成绩列表： 所有学期成绩  ，其余的用空值填充
	 * @return
	 */
	public static MTermList.Builder generateForOne(String url){
		MTermList.Builder list = MTermList.newBuilder();
		MTerm.Builder term = MTerm.newBuilder();
		term.setName("所有学期成绩");
		term.setUrl(url);
		list.addTerm(term);
		fillList(list);
		return list;
	}
	
	/**
	 * 产生有两个项成绩列表：所有学期成绩      本学期成绩    ，其余的用空值填充 
	 * @param termUrl
	 * @param allUrl
	 * @return
	 */
	public static MTermList.Builder generateForTwo(String termUrl, String allUrl){
		MTermList.Builder list = MTermList.newBuilder();
		MTerm.Builder term = MTerm.newBuilder();
		term.setName("本学期成绩");
		term.setUrl(termUrl);
		list.addTerm(term);
		
		term = MTerm.newBuilder();
		term.setName("所有学期成绩");
		term.setUrl(allUrl);
		list.addTerm(term);
		fillList(list);
		return list;
	}
	
	/**
	 * 产生有两个项成绩列表：   本学期成绩    所有学期成绩     （曾）不及格成绩   ，其余的用空值填充     
	 * @param termUrl
	 * @param allUrl
	 * @return
	 */
	public static MTermList.Builder generateForThree(String termUrl, String allUrl, String failUrl){
		MTermList.Builder list = MTermList.newBuilder();
		MTerm.Builder term = MTerm.newBuilder();
		term.setName("本学期成绩");
		term.setUrl(termUrl);
		list.addTerm(term);
		
		term = MTerm.newBuilder();
		term.setName("所有学期成绩");
		term.setUrl(allUrl);
		list.addTerm(term);

		term = MTerm.newBuilder();
		term.setName("（曾）不及格成绩 ");
		term.setUrl(failUrl);
		list.addTerm(term);
		fillList(list);
		return list;
	}
	
	
	/**
	 * 将不足10条的学期列表填充满
	 * @param list
	 */
	public static void fillList(MTermList.Builder list){
		for(int i = list.getTermCount() ; i < 10 ; i++){
			MTerm.Builder term = MTerm.newBuilder();
			term.setName("");
			term.setUrl("");
			list.addTerm(term);
		}
	}

}
